package com.yantong.controller;

import com.yantong.annotation.Log;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.Message;
import com.yantong.pojo.MessageType;
import com.yantong.service.MessageService;
import com.yantong.utils.ValidateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 消息管理控制类
 * @Auther: javaMan
 * @Date: 2020/08/21
 * @Description:
 */

@Controller
@RequestMapping("/sys/message")
public class MessageController extends BaseController {
    private static final String prefix = "system/message";
    @Autowired
    private MessageService messageService;

    /**
     * 消息类型页面
     */
    @RequiresPermissions("sys:message:messageType")
    @GetMapping("/messageType")
    public String messageType() {
        return prefix + "/messageType";
    }

    /**
     * 消息类型列表
     */
    @ResponseBody
    @RequiresPermissions("sys:message:messageType")
    @GetMapping("/messageTypeList")
    public PageUtils<MessageType> messageTypeList(int offset, int limit) {
        return messageService.messageTypeList(offset, limit);
    }

    /**
     * 新增消息类型页面
     */
    @RequiresPermissions("sys:message:addMessageType")
    @GetMapping("/addMessageTypePage")
    public String addMessageTypePage() {
        return prefix + "/addMessageTypePage";
    }

    /**
     * 消息类型新增操作
     */
    @Log(module = "消息类型管理", operation = "新增消息类型")
    @ResponseBody
    @RequiresPermissions("sys:message:addMessageType")
    @PostMapping("/addMessageType")
    public ResultVo addMessageType(MessageType messageType) {
        if (messageType == null) {
            return ResultVo.error();
        }
        if (messageService.addMessageType(messageType) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 删除消息类型
     */
    @Log(module = "消息类型管理", operation = "删除消息类型")
    @ResponseBody
    @RequiresPermissions("sys:message:deleteMessageType")
    @PostMapping("/deleteMessageType")
    public ResultVo deleteMessageType(Long type_id) {
        if (type_id == null) {
            return ResultVo.error();
        }
        if (messageService.deleteMessageType(type_id) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }


    /**
     * 后台消息页面
     */
    @RequiresPermissions("sys:message:backMessage")
    @GetMapping("/backMessage")
    public String backMessage() {
        return prefix + "/backMessage";
    }

    /**
     * 后台消息列表
     */
    @ResponseBody
    @RequiresPermissions("sys:message:backMessage")
    @GetMapping("/backMessageList")
    public PageUtils<Message> backMessageList(int offset, int limit) {
        Long centreId=getUserCentreId();
        if(ValidateUtil.isEmptyObjectOrString(centreId)){
            return messageService.backMessageList(offset, limit);
        }
        else {
            return messageService.backMessageListByCentreId(centreId,offset, limit);
        }
    }

    /**
     * 新增后台消息页面
     */
    @RequiresPermissions("sys:message:addMessageBack")
    @GetMapping("/addMessageBackPage")
    public String addMessageBackPage(Model model) {
        model.addAllAttributes(messageService.getMessageTypeListToMap());//消息类型
        Long centreId=getUserCentreId();
        //model.addAllAttributes(messageService.getDivisionListToMap());//获取事业部列表
        if(ValidateUtil.isEmptyObjectOrString(centreId)){
            model.addAllAttributes(messageService.getCentreIdListToMap());//获取user列表
        }
        else {
            model.addAllAttributes(messageService.getCentreById(centreId));
        }
        return prefix + "/addMessageBackPage";
    }

    /**
     * 后台消息新增操作
     */
    @Log(module = "后端消息管理", operation = "后端新增消息")
    @ResponseBody
    @RequiresPermissions("sys:message:addMessageBack")
    @PostMapping("/addMessageBack")
    public ResultVo addMessageBack(Message message) {
        if (message == null) {
            return ResultVo.error();
        }
        Long centreId=getUserCentreId();
        if(ValidateUtil.isEmptyObjectOrString(message.getCentreId()) && ValidateUtil.isNotEmptyObjectOrString(centreId)) {
            message.setCentreId(centreId);
        }
        if (messageService.addMessageBack(message) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 更新后台消息页面
     */
    @RequiresPermissions("sys:message:updateMessageBack")
    @GetMapping("updateMessageBackPage/{id}")
    public String updateMessageBackPage(@PathVariable Long id, Model model) {
        model.addAllAttributes(messageService.getMessageTypeListToMap());
        Long centreId=getUserCentreId();
        //model.addAllAttributes(messageService.getDivisionListToMap());//获取事业部列表
        if(ValidateUtil.isEmptyObjectOrString(centreId)){
            model.addAllAttributes(messageService.getCentreIdListToMap());//获取user列表
        }
        else {
            model.addAllAttributes(messageService.getCentreById(centreId));
        }
        model.addAttribute("message", messageService.getMessageBackPrimaryKey(id));
        return prefix + "/updateMessageBackPage";
    }

    /**
     * 更新后台消息操作
     */
    @Log(module = "后端消息管理", operation = "后端修改消息")
    @ResponseBody
    @RequiresPermissions("sys:message:updateMessageBack")
    @PostMapping("/updateMessageBack")
    public ResultVo updateMessageBack(Message message) {
        if (message == null || message.getId() == null) {
            return ResultVo.error();
        }
        Long centreId=getUserCentreId();
        if(ValidateUtil.isEmptyObjectOrString(message.getCentreId()) && ValidateUtil.isNotEmptyObjectOrString(centreId)) {
            message.setCentreId(centreId);
        }
        if (messageService.updateMessageBack(message) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 删除后台消息
     */
    @Log(module = "消息管理", operation = "删除消息")
    @ResponseBody
    @RequiresPermissions("sys:message:deleteMessageBack")
    @PostMapping("/deleteMessageBack")
    public ResultVo deleteMessageBack(Long id) {
        if (id == null) {
            return ResultVo.error("id不能为空！");
        }
        if (messageService.deleteMessageBack(id) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 发送后台消息
     */
    @Log(module = "消息管理", operation = "发送消息")
    @ResponseBody
    @RequiresPermissions("sys:message:sendMessageBack")
    @PostMapping("/sendMessageBack")
    public ResultVo sendMessageBack(Long id) {
        if (id == null) {
            return ResultVo.error("id不能为空！");
        }
        if (messageService.sendMessageBack(id) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }


}
